package com.miaoxun.controller;


import com.miaoxun.common.encrypt.Encrypt;

import com.miaoxun.common.file.ExcelUtil;
import com.miaoxun.domain.entity.User;
import com.miaoxun.domain.enums.ErrEnum;
import com.miaoxun.common.util.UrlPath;
import com.miaoxun.domain.entity.Menu;
import com.miaoxun.domain.model.ResultModel;
import com.miaoxun.domain.model.Tree;
import com.miaoxun.domain.param.LoginParam;
import com.miaoxun.service.MenuService;
import com.miaoxun.service.UserService;
import com.miaoxun.service.queue.Producer;
import com.miaoxun.web.shiro.ShiroUtils;
import com.miaoxun.web.util.StreamUtil;
import io.swagger.annotations.ApiOperation;

import io.swagger.annotations.ApiParam;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import javax.validation.Valid;
import java.awt.print.Book;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Controller
public class IndexController extends BaseController {
    @Autowired
    MenuService menuService;

    @ApiOperation(value = "登录", notes = "登录")
    @GetMapping(value = UrlPath._LOGIN)
    public String Login() {
        return "login";
    }

    @ApiOperation(value = "登录", notes = "登录")
    @ResponseBody
    @PostMapping(value = UrlPath._LOGIN)
    public ResultModel Login(@Valid LoginParam userParam) {
        UsernamePasswordToken token = new UsernamePasswordToken(userParam.getUsername(), Encrypt.LoginPwd.Encode(userParam.getPassword()));
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        return ResultModel.SUCCESS(ErrEnum.SUCCESS.getCode(), ErrEnum.SUCCESS.getMsg());
    }

    @ApiOperation(value = "首页", notes = "首页")
    @GetMapping(value = {UrlPath._INDEX})
    public String index(Model model) {
        if (getUserId() == null)
            return "redirect:login";
        List<Tree<Menu>> menus = menuService.listMenuTree(getUserId());
        model.addAttribute("menus", menus);
        return UrlPath.INDEX;
    }

    @GetMapping(value = "/logout")
    public String logout() {
        ShiroUtils.logout();
        return "login";
    }

    @GetMapping("/main")
    public String main() {
        return "main";
    }

    @ApiOperation(value = "权限说明", notes = "权限")
    @GetMapping("/shrio")
    @ResponseBody
    public String shrio() {
        return "shiro:hasPermission=  路径斜线转:   有权限显示  无权限隐藏 ";
    }

    @Autowired
    private Producer producer;



    @GetMapping("/test")
    @ResponseBody
    public void test(HttpServletResponse response) throws Exception {

//        Workbook workbook = ExcelUtil.CreateExcel(userService.getAll());
//        StreamUtil.RESPONSE(response, workbook, userService.getClass().getSimpleName()+".xls");
    }


    @PostMapping("/test2")
    @ResponseBody
    public  List<? extends Serializable> test2(@ApiParam(value = "file", required = true, name = "file") @RequestParam("file") MultipartFile file)  {

        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();

        } catch (IOException e) {
            e.printStackTrace();
        }
        List<? extends Serializable> serializables = null;

            serializables = ExcelUtil.CreateData(inputStream, User.class);

        return serializables;
    }

}
